//
//  SearchView.m
//  ShopDemo
//
//  Created by Sunshine on 2018/8/15.
//  Copyright © 2018年 飞扬的青春. All rights reserved.
//

#import "SearchView.h"
#import "XSCollectionCell.h"
@implementation SearchView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatCollectionviewMethod];
//        [self showXSButtonImageDataMethodWithUrl:[MethodClass returnUrlSetStrWSDLURL:@"/dashop/seckill/app/getSecKill.do"]];
        
    }
    return self;
}


////限时抢购按钮图
//-(void)showXSButtonImageDataMethodWithUrl:(NSString *)url{
//    [YQNetworking getWithUrl:url refreshRequest:YES cache:NO params:nil progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
//    } successBlock:^(id response) {
//        self.dataArray = response;
//        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
//            [self.listCollectionview reloadData];
//        }];
//    } failBlock:^(NSError *error) {
//
//    }];
//
//}


-(void)creatCollectionviewMethod{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.listCollectionview = [[ UICollectionView alloc ] initWithFrame:CGRectMake(0, 1, kScreenWidth, 200) collectionViewLayout :layout];
    self.listCollectionview.showsHorizontalScrollIndicator = NO;
    self.listCollectionview.alwaysBounceHorizontal = NO;
    [self addSubview:self.listCollectionview];
    if (kScreenHeight == 812) {
        self.listCollectionview.sd_layout
        .bottomSpaceToView(self, 34);
    }
    
    self.listCollectionview.delegate = self;
    self.listCollectionview.dataSource = self;
    self.listCollectionview.backgroundColor = [UIColor whiteColor];
    [self.listCollectionview registerClass :[ XSCollectionCell class ] forCellWithReuseIdentifier : @"onecell" ];
}


#pragma mark--------------------UICollectionViewDelegateFlowLayout,UICollectionViewDelegate, UICollectionViewDataSource
//cell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
//
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    XSCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"onecell" forIndexPath:indexPath];
    NSDictionary *dic = self.dataArray[indexPath.row];
    cell.titlab.text = [NSString stringWithFormat:@"%@",dic[@"NAME"]];
    cell.titlab.textColor = [UIColor orangeColor];
    cell.titlab.layer.masksToBounds = YES;
    cell.titlab.layer.cornerRadius = 5*kJLXWidthScale;
    cell.titlab.layer.borderWidth = 1;
    cell.titlab.layer.borderColor = LineColor.CGColor;
    cell.titlab.sd_layout
    .topSpaceToView(cell.contentView, 5*kJLXWidthScale)
    .bottomSpaceToView(cell.contentView, 5*kJLXWidthScale)
    .leftSpaceToView(cell.contentView, 8*kJLXWidthScale)
    .rightSpaceToView(cell.contentView, 8*kJLXWidthScale);
    return cell;
}


#pragma mark-------------------布局代理
//设置每个cell的宽和高
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = self.dataArray[indexPath.row];
    NSString *contentStr = dic[@"NAME"];
     CGRect rect = [contentStr boundingRectWithSize:CGSizeMake(MAXFLOAT, 30) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]} context:nil];
    return CGSizeMake(rect.size.width+20*kJLXWidthScale, 45*kJLXWidthScale);
    
}
//设置分区缩进
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10*kJLXWidthScale, 8*kJLXWidthScale, 0, 8*kJLXWidthScale);
}
//设置每个分区之间最小的行间距和列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 1;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 1;
}
#pragma mark--------------------------cell点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = self.dataArray[indexPath.row];
    self.headerBlock(dic, indexPath.row);
    
    
}

-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
//    XSCollectionCell * cell = (XSCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];
//    cell.titlab.textColor = [UIColor orangeColor];
}

@end
